﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QuickSL;
using System.Threading;

namespace SLDemo
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            BackgroundQueue.Current.IsBusyChanged += new EventHandler(Current_IsBusyChanged);
        }

        void Current_IsBusyChanged(object sender, EventArgs e)
        {
            bsyIndicator.IsBusy = BackgroundQueue.Current.IsBusy;
        }

        private bool _isReset = false;
        private void btnReset_Click(object sender, RoutedEventArgs e)
        {
            _isReset = true;
            DispatcherQueue.Current.Clear();
        }

        private void btnNormal_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("This may cause the IE to crack!", "Alert", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                _isReset = false;
                for (int i = 0; i < 1000; i++)
                {
                    string count = i.ToString();
                    Dispatcher.BeginInvoke(() =>
                    {
                        Add();
                        txtCount.Text = count;
                    });

                    if (_isReset)
                        break;
                }
            }
        }

        private void btnQuickSL_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 1000; i++)
            {
                string count = i.ToString();
                DispatcherQueue.Current.Enqueue(() =>
                {
                    Add();
                    txtCount.Text = count;
                });
            }
        }

        private void Add()
        {
            recLayout.Children.Add(GetRectangle((int)recLayout.ActualWidth,(int)recLayout.ActualHeight));

            Thread.Sleep(10);//simulate a long-time process
        }

        private Random _rndPosition = new Random(DateTime.Now.Millisecond);
        private Rectangle GetRectangle(int maxX, int maxY)
        {
            Rectangle ctrl = new Rectangle();
            ctrl.Width = 5;
            ctrl.Height = 5;
            ctrl.Fill = new SolidColorBrush(Colors.Red);
            ctrl.HorizontalAlignment = HorizontalAlignment.Left;
            ctrl.VerticalAlignment = VerticalAlignment.Top;
            ctrl.Margin = new Thickness(_rndPosition.Next(0, maxX), _rndPosition.Next(0, maxY), 0, 0);
            return ctrl;
        }

        private BackgroundQueueItem _currentLoadItem = null;
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            _currentLoadItem = BackgroundQueue.Current.Enqueue(
                () =>
                    {
                        Thread.Sleep(1000);
                    },
                () =>
                    {
                        ntLayout.Children.Add(GetRectangle((int)ntLayout.ActualWidth, (int)ntLayout.ActualHeight));
                        btnLoad.IsEnabled = true;
                        _currentLoadItem = null;
                    });
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            if(_currentLoadItem!=null)
            {
                BackgroundQueue.Current.Cancel(_currentLoadItem);
            }
        }
    }
}
